<?php

use \Predis\Client;

/**
 * 获取会员号
 * @return string
 */
function getCustomerNo() {

    $redisConfig = config("constants.redis_config");
    $redis = new Client($redisConfig);

    return $redis->lpop("customerNo");

}

/**
 * 设置首页缓存
 * @param $list
 * @return int
 */
function setIndexGoodsCache($list) {

    $redisConfig = config("constants.redis_config");
    $redis = new Client($redisConfig);

    return $redis->set("indexGoodsCache", json_encode($list));

}

/**
 * 获取首页Goods缓存
 * @return string
 */
function getIndexGoodsCache() {

    $redisConfig = config("constants.redis_config");
    $redis = new Client($redisConfig);

    return $redis->get("indexGoodsCache");

}

/**
 * 设置最后拉取时间
 * @param $time
 * @param null $redisClient
 * @return mixed
 */
function setXdLastPullTime($time, $redisClient = null) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    return $redisClient->set("xdOrderLastPullTime", $time);

}

/**
 * 获取最后拉取时间
 * @param null $redisClient
 * @return string
 */
function getXdLastPullTime($redisClient = null) {


    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    return $redisClient->get("xdOrderLastPullTime");

}

/**
 * 设置更新商品最后拉取时间
 * @param $time
 * @return int
 */
function setXdGoodsLastPullTime($time) {

    $redisConfig = config("constants.redis_config");
    $redis = new Client($redisConfig);

    return $redis->set("xdGoodsLastPullTime", $time);

}

/**
 * 获取更新商品最后拉取时间
 * @return string
 */
function getXdGoodsLastPullTime() {

    $redisConfig = config("constants.redis_config");
    $redis = new Client($redisConfig);

    return $redis->get("xdGoodsLastPullTime");

}

/**
 * 放入用户formId
 * @param $customerId
 * @param $formId
 * @return int
 */
function pushCustomerFormId($customerId, $formId) {

    $redisConfig = config("constants.redis_config");
    $redisConfig["database"] = 1;

    $redis = new Client($redisConfig);

    $data = [
        "formId" => $formId,
        "time" => time()
    ];

    return $redis->lpush("customerFormId:$customerId", array(json_encode($data)));

}

/**
 * 添加销售榜
 * @param $mchCustomerId
 * @param $consumeCustomerId
 * @param $money
 * @param null $redisClient
 * @return int
 */
function addConsumeAmt($mchCustomerId, $consumeCustomerId, $money, $redisClient = null) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    return $redisClient->incrbyfloat("addConsumeAmt:$mchCustomerId:$consumeCustomerId", $money);

}

/**
 * 添加总单量
 * @param $customerId
 * @param null $redisClient
 * @return string
 */
function addTotalSettleNumber($customerId, $redisClient = null) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    return $redisClient->incr("addTotalSettleNumber:$customerId");

}

/**
 * 减去余额
 * @param $customerId
 * @param $money
 * @param null $redisClient
 * @return string
 */
function reduceTotalAssets($customerId, $money, $redisClient = null) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    return $redisClient->incrbyfloat("addTotalAssets:$customerId", $money*(-1));
}

/**
 * 添加总余额
 * @param $customerId
 * @param $money
 * @param null $redisClient
 * @return string
 */
function addTotalAssets($customerId, $money, $redisClient = null) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    return $redisClient->incrbyfloat("addTotalAssets:$customerId", $money);

}

/**
 * 添加历史收益金额
 * @param $customerId
 * @param $money
 * @param null $redisClient
 * @return string
 */
function addHistoryProfit($customerId, $money, $redisClient = null) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    return $redisClient->incrbyfloat("addHistoryProfit:$customerId", $money);

}

/**
 * 添加399团队成员
 * @param $customerId
 * @param null $redisClient
 * @return int
 */
function addRecommendNumber($customerId, $redisClient = null) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    return $redisClient->incr("addRecommendNumber:$customerId");

}

/**
 * 取用户formId
 * @param $customerId
 * @param null $redisClient
 * @return array
 */
function popCustomerFormId($customerId, $redisClient = null) {

    $returnData = [
        "success" => false,
        "formId" => ""
    ];

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisConfig["database"] = 1;

        $redisClient = new Client($redisConfig);

    }

    $data = $redisClient->lpop("customerFormId:$customerId");

    if(!isNullOrEmpty($data)) {

        $param = json_decode($data, true);

        if(floor(time() - $param["time"])/86400 > 6) {
            return popCustomerFormId("customerFormId:$customerId", $redisClient);
        } else {
            $returnData["success"] = true;
            $returnData["formId"] = $param["formId"];
        }

    }

    return $returnData;

}

/**
 * 清除手机号验证码
 * @param $mobile
 * @return int
 */
function clearMobileCode($mobile) {

    $redis = new Client(config("constants.redis_config"));

    return $redis->hdel("smsRegister", $mobile);

}

/**
 * 设置手机号验证码
 * @param $mobile
 * @param $code
 * @return int
 */
function setMobileCode($mobile, $code) {

    $redis = new Client(config("constants.redis_config"));

    return $redis->hset("smsRegister", $mobile, $code);

}

/**
 * 获取手机号验证码
 * @param $mobile
 * @return string
 */
function getMobileCode($mobile) {

    $redis = new Client(config("constants.redis_config"));

    return $redis->hget("smsRegister", $mobile);

}

/**
 * 存储阿里云 临时授权token
 * @param $value
 * @return int
 */
function setRedisOssToken($value) {

    $redis = new Client(config("constants.redis_config"));

    return $redis->setex("ossToken", 3000, json_encode($value));

}

/**
 * 获取阿里云 临时授权token
 * @return string
 */
function getRedisOssToken() {

    $redis = new Client(config("constants.redis_config"));

    return $redis->get("ossToken");
}

/**
 * 获取指定日期有统计数据的用户总数
 * @param $date
 * @param null $redisClient
 * @return int
 */
function getStatisticsCustomerCount($date, $redisClient = null)
{
    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    return $redisClient->scard("customerEverydayStatistics:$date");
}

/**
 * 指定日期有统计数据的用户id添加到redis
 * @param $customerId
 * @param $date
 * @param null $redisClient
 * @return int
 */
function addStatisticsCustomerId($customerId, $date = null, $redisClient = null)
{
    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    if ($date === null) {
        $date = date("Y-m-d");
    }

    return $redisClient->sadd("customerEverydayStatistics:$date", $customerId);
}

/**
 * 获取一个指定日期有统计数据的用户id
 * @param $date
 * @param null $redisClient
 * @return string
 */
function popStatisticsCustomerId($date, $redisClient = null)
{
    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);

    }

    return $redisClient->spop("customerEverydayStatistics:$date");
}

/**
 * 存入优选分类
 * @param $data
 * @param null $redisClient
 * @return mixed
 */
function setYxGoodsCat($data, $redisClient = null ) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);
    }

    return $redisClient->set("yxGoodsCat", $data);
}

/**
 * 获取优选分类
 * @param null $redisClient
 * @return string
 */
function getYxGoodsCat($redisClient = null) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);
    }

    return $redisClient->get("yxGoodsCat");
}

/**
 * 存入小店图片素材
 * @param $data
 * @param null $redisClient
 * @return mixed
 */
function setXdPictureMaterial($data, $redisClient = null) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);
    }

    return $redisClient->set("xdPictureMaterial", $data);
}

/**
 * 获取小店图片素材
 * @param null $redisClient
 * @return mixed
 */
function getXdPictureMaterial($redisClient = null) {

    if($redisClient == null) {

        $redisConfig = config("constants.redis_config");
        $redisClient = new Client($redisConfig);
    }

    return $redisClient->get("xdPictureMaterial");
}